package rpc

import (
	"cluster-cache/common"
)

// TcpTransport 同时包含tcp server, tcp client, 用于节点之间互通数据
type TcpTransport struct {
	Client *TcpClient
	Server *TcpServerV1

	// lastIndex 全局共享最新索引
	lastIndex *common.LastIndex
}

const MaxConnectionPerHost = 10

func NewTcpTransport(urls []string, id int, hand Handler) *TcpTransport {
	lastIndex := common.NewLastIndex()
	client := NewTcpClient(urls, MaxConnectionPerHost, lastIndex)
	server := NewTcpServerV1(urls[id], hand)
	go server.Start()
	return &TcpTransport{Client: client, Server: server, lastIndex: lastIndex}
}

func (t *TcpTransport) LoadLastIndex() uint64 {
	return t.lastIndex.Get()
}

func (t *TcpTransport) SetLastIndex(index uint64) {
	t.lastIndex.Set(index)
}
